package com.lxmes.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.base.domain.JcItembzinfo;
import com.lxmes.business.domain.CcRukuDetail;
import com.lxmes.common.annotation.RepeatSubmit;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.business.domain.CcRukuInfo;
import com.lxmes.business.service.ICcRukuInfoService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 入库单Controller
 *
 * @author 张志煜
 * @date 2022-11-27
 */
@Api(tags="入库单管理")
@RestController
@RequestMapping("/business/rukuInfo")
public class CcRukuInfoController extends BaseController
{
    @Autowired
    private ICcRukuInfoService ccRukuInfoService;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 查询入库单列表
     */
    @ApiOperation(value="入库单列表")
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcRukuInfo ccRukuInfo)
    {
        startPage();
        List<CcRukuInfo> list = ccRukuInfoService.selectCcRukuInfoList(ccRukuInfo);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:mainList')")
    @GetMapping("/mainList")
    public TableDataInfo mainList(CcRukuInfo ccRukuInfo)
    {
        startPage();
        List<CcRukuInfo> list = ccRukuInfoService.selectCcRukuInfoMainList(ccRukuInfo);
        return getDataTable(list);
    }
    /**
     * 查询入库结算单列表
     */
    @ApiOperation(value="入库单列表")
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:jiesuanList')")
    @GetMapping("/jiesuanList")
    public TableDataInfo jiesuanList(CcRukuInfo ccRukuInfo)
    {
        startPage();
        List<CcRukuInfo> list = ccRukuInfoService.selectJieSuanCcRukuInfoList(ccRukuInfo);
        return getDataTable(list);
    }


    /**
     * 导出入库单列表
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:export')")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcRukuInfo ccRukuInfo)
    {
        List<CcRukuInfo> list = ccRukuInfoService.selectCcRukuInfoList(ccRukuInfo);
        ExcelUtil<CcRukuInfo> util = new ExcelUtil<CcRukuInfo>(CcRukuInfo.class);
        util.exportExcel(response, list, "入库单数据");
    }
    @Log(title = "入库单导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<CcRukuInfo> util = new ExcelUtil<>(CcRukuInfo.class);
        List<CcRukuInfo> ccRukuInfoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = ccRukuInfoService.imporIteminfo(ccRukuInfoList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<CcRukuInfo> util = new ExcelUtil<>(CcRukuInfo.class);
        util.importTemplateExcel(response, "入库单");
    }

    /**
     * 获取入库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ccRukuInfoService.selectCcRukuInfoById(id));
    }
    @ApiOperation(value="整单入库")
    @PostMapping(value = "/zdRuKu")
    @Log(title = "整单入库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    public AjaxResult zdRuKu(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return AjaxResult.success(ccRukuInfoService.zdRuKu(ccRukuInfo));
    }

    /**
     * 新增入库单
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:add')")
    @Log(title = "手动入库", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.insertCcRukuInfo(ccRukuInfo));
    }


    @PreAuthorize("@ss.hasPermi('business:rukuInfo:add')")
    @PostMapping("/addPurRukuInfo")
    @Log(title = "采购订单入库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    public AjaxResult addPurRukuInfo(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.insertPurCcRukuInfo(ccRukuInfo));
    }
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:add')")
    @PostMapping("/addCkRukuInfo")
    @Log(title = "出库单流转入库", businessType = BusinessType.INSERT)
    @RepeatSubmit
    public AjaxResult addCkRukuInfo(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.insertCkCcRukuInfo(ccRukuInfo));
    }

    /**
     * 扫码采购订单入库
     */
    @ApiOperation(value="扫码采购订单入库")
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:add')")
    @Log(title = "扫码采购订单入库", businessType = BusinessType.INSERT)
    @PostMapping("/mobileSmAddPurRk")
    @RepeatSubmit
    public AjaxResult mobileSmAddPurRk(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.insertSmPurCcRukuInfo(ccRukuInfo));
    }

    /**
     * 扫码入库
     */
    @ApiOperation(value="扫码入库")
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:add')")
    @Log(title = "扫码入库", businessType = BusinessType.INSERT)
    @PostMapping("/mobileSmAdd")
    @RepeatSubmit
    public AjaxResult mobileSmAdd( CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.insertSmCcRukuInfo(ccRukuInfo));
    }
    @ApiOperation(value="获取入库类型")
    @GetMapping("/getDictDataRuKuType")
    public AjaxResult getDictDataRuKuType(){
        return AjaxResult.success( dictDataService.selectDictDataByType("ruku_type"));
    }
    @ApiOperation(value="获取入库单位")
    @GetMapping("/getDictDataRuKuUnit")
    public AjaxResult getDictDataRuKuUnit(){
        return AjaxResult.success( dictDataService.selectDictDataByType("jc_iteminfo_unit"));
    }

    /**
     * 添加采购物料明细
     */
    @GetMapping("/addRukuItemInfo/{ids}")
    public AjaxResult addRukuItemInfo(@PathVariable Long[] ids)
    {
        List<CcRukuInfo> list = ccRukuInfoService.addRukuItemInfo(ids);
        return AjaxResult.success(list);
    }

    /**
     * 添加采购物料明细
     */
    @GetMapping("/addPurRukuItemInfo/{ids}")
    public AjaxResult addPurRukuItemInfo(@PathVariable Long[] ids)
    {
        List<CcRukuInfo> list = ccRukuInfoService.addPurRukuItemInfo(ids);
        return AjaxResult.success(list);
    }
    /**
     * 添加采购物料明细
     */
    @GetMapping("/addChukuRukuItemInfo/{ids}")
    public AjaxResult addChukuRukuItemInfo(@PathVariable Long[] ids)
    {
        List<CcRukuInfo> list = ccRukuInfoService.addChukuRukuItemInfo(ids);
        return AjaxResult.success(list);
    }


    @GetMapping("/zhixingRuku/{ids}")
    public AjaxResult zhixingRuku(@PathVariable Long[] ids)
    {
        return AjaxResult.success(ccRukuInfoService.zhixingRuku(ids));
    }


    /**
     * 修改入库单
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CcRukuInfo ccRukuInfo)
    {
        return toAjax(ccRukuInfoService.updateCcRukuInfo(ccRukuInfo));
    }

    /**
     * 撤销入库单
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:remove')")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ccRukuInfoService.deleteCcRukuInfoByIds(ids));
    }
    /**
     * 删除入库单
     */
    @PreAuthorize("@ss.hasPermi('business:rukuInfo:remove')")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{ids}")
    public AjaxResult remove2(@PathVariable Long[] ids)
    {
        return toAjax(ccRukuInfoService.deleteCcRukuInfoByIds2(ids));
    }
}
